package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.ITeacherService;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

/**
 * 老师用户交互类
 */
public abstract class BaseTeacherController {
    private ITeacherService service = new TeacherService();
    private Scanner sc = new Scanner(System.in);

    //老师交互类中入口
    public final void start() {
        // 死循环让用户选择
        //Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师   5.退出");


            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    // System.out.println("添加");
                    addTeacher();
                    break;
                case 2:
                    //System.out.println("删除");
                    deleteTeacher();
                    break;
                case 3:
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case 4:
                    //System.out.println("查询");
                    findAllTeachers();
                    break;
                case 5:
                    System.out.println("谢谢使用老师管理系统");
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }

        }


    }

    //录入工号
    public final String getSid() {
        String sid;
        while (true) {
            System.out.println("请输入老师工号");
            sid = sc.next();

            // 判断工号是否存在
            boolean exists = service.isExists(sid);
            if (!exists) {
                // 不存在重新录
                System.out.println("输入的工号 不存在, 请重新选择或输入exit退出");
            } else {
                // 存在去删除
                break;
            }

        }
        return sid;
    }

    //获取老师信息  ctrl+alt+m  抽取方法
    public abstract Teacher getTeacher(String sId) ;

    private final void updateTeacher() {
        String sid = getSid();
        if ("exit".equals(sid)) {
            return;
        }
       /* System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Teacher teacher = new Teacher();
        teacher.setSid(sid);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);*/
        Teacher teacher = getTeacher(sid);
        //创建service 对象

        //修改
        service.updateTeacher(sid, teacher);
        System.out.println("修改成功");
    }

    public final void deleteTeacher() {

        /*String deleteId;
        while (true) {
            System.out.println("请输入要删除的老师工号");
            deleteId = sc.next();
            if ("exit".equals(deleteId)) {
                return;
            }
            // 判断工号是否存在
            boolean exists = service.isExists(deleteId);
            if (!exists) {
                // 不存在重新录
                System.out.println("要删除的工号 不存在, 请重新选择或输入exit退出");
            } else {
                // 存在去删除
                break;
            }
        }
*/
        String sid = getSid();
        if ("exit".equals(sid)) {
            return;
        }
        service.deleteTeacher(sid);
        System.out.println("删除成功!");


    }

    public final void findAllTeachers() {
        Teacher[] teachers = service.findAllTeachers();
        if (teachers == null) {
            System.out.println("没有老师信息, 请先添加");
            return;
        }
        System.out.println("工号\t姓名\t年龄\t生日");
        for (int i = 0; i < teachers.length; i++) {
            //teachers.get(i)
            Teacher teacher = teachers[i];
            if (teacher != null) {

                System.out.println(teacher.getSid() + "\t" + teacher.getName() + "\t" + teacher.getAge() + "岁\t" + teacher.getBirthday());
            }
        }


    }

    public final void addTeacher() {

        // Scanner sc = new Scanner(System.in);
        String sId;
        while (true) {
            //	① 显示提示信息，提示要输入何种信息
            //     * 		② 键盘录入老师对象所需要的数据
            System.out.println("请输入工号");
            sId = sc.next();

            boolean flag = service.isExists(sId);
            if (flag) {
                System.out.println("输入的工号已存在");
            } else {
                break;
            }

        }

        Teacher teacher = getTeacher(sId);
        //创建service 对象

        //调用service 方法
        boolean result = service.addTeacher(teacher);
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }


    }

    //删除方式2
    public final void deleteTeacher2(){
        while(true){
            //用户录入工号
            System.out.println("请输入要删除的工号");

            String sid = sc.next();
            //判断工号所在的索引
            int index=service.getIndex(sid);

            // 如果时-1  没有
            if(index==-1){
                //重新录
                System.out.println("录入的工号不存在 ， 请重新输入");
            }else{
                service.deleteTeacherByIndex(index);
                System.out.println("删除成功");
                break;

            }
            //不等于-1 调用service  删除方法

        }




    }


}
